/**
 * Created by Administrator on 2018/1/30.
 */

/**
 * 动态模板加载
 * 依赖
 * jquery  template7
 * @type {{}}
 */

Camera = function (param) {
    if (!param.upload_url) {
        param.upload_url = url_("index/Tool/upload");
    }
    if (!param.callback) {
        console.error("Camera not config callback");
        return;
    }
    if (!window.plus) {
        return;
    }
    //压缩
    var compressImage = function (url) {
        plus.zip.compressImage({
                src: url,//src: (String 类型 )压缩转换原始图片的路径
                dst: url,//压缩转换目标图片的路径
                quality: 20,//quality: (Number 类型 )压缩图片的质量.取值范围为1-100
                overwrite: true//overwrite: (Boolean 类型 )覆盖生成新文件
            },
            function (event) {
                var upload_img_url = param.upload_url;
                var wa = plus.nativeUI.showWaiting();
                var task = plus.uploader.createUpload(upload_img_url, {
                        method: "POST"
                    },
                    function (t, status) {
                        param.responseText = t.responseText;
                        param.response = JSON.parse(t.responseText);
                        param.callback(param);
                        wa.close();
                    }
                );
                task.addFile(url, {key: 'file'});
                task.start();

            }, function (error) {
                plus.nativeUI.toast("请稍候再试");
            });
    }
    var cmr = plus.camera.getCamera();
    cmr.captureImage(function (p) {
        plus.io.resolveLocalFileSystemURL(p, function (entry) {
            compressImage(entry.toLocalURL());
        }, function (e) {
            my_tips_('读取拍照文件错误：' + e.message);
        });
    }, function (e) {
        my_tips_('失败：' + e.message);
    }, {filename: '_doc/camera/', index: 1});
};
$.extend({
    camera: Camera
});